package com.snail.billing.net;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;

public abstract class BillingTreeArrayHttpSession extends BillingHttpSession {

	@Override
	public void buildParamPair() {
		BillingSecurity security = getSecurity();
		addParamPair("accessId", security.accessId);
		addParamPair("accessType", security.accessType);
		for (NameValuePair pair : getBillingPairList()) {
			addParamPair(pair.getName(), pair.getValue());
		}
		addParamPair("sign", buildSign(security, getBillingPairList()));
	}

	protected String buildSign(BillingSecurity security, List<NameValuePair> list) {
		StringBuilder builder = new StringBuilder();
		Map<String, String> map = new TreeMap<String, String>();
		for (NameValuePair pair : list) {
			map.put(pair.getName(), pair.getValue());
		}
		for (String key : map.keySet()) {
			builder.append(key);
			builder.append(map.get(key));
		}
		builder.append(security.seed);
		String origin = builder.toString();
		return BillingEncode.MD5(origin);
	}

}
